/*
 * Copyright 2011 Subhabrata Ghosh <subho.ghosh at langene.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



package net.langene.nlp;

//~--- non-JDK imports --------------------------------------------------------

import net.langene.nlp.scripts.DBSetup;

import org.apache.commons.configuration.XMLConfiguration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//~--- JDK imports ------------------------------------------------------------

import java.sql.Connection;
import java.sql.DriverManager;

//~--- classes ----------------------------------------------------------------

/**
 * @author subhagho
 *
 */
public class Context {
    private static Logger	_logger = null;

    /*
     * Context Instance Section
     */
    private static final Context	_ctx = new Context();

    //~--- fields -------------------------------------------------------------

    private String	language = "en";
    private Connection	dbconn;

    //~--- methods ------------------------------------------------------------

    /*
     * End Context Instance Section
     */

    /**
     * Create the application context. - Load configuration, Initialize Logging
     *
     * @param configfile
     *            - Application configuration filename (XML)
     * @throws Exception
     */
    public static final void create(String configfile) throws Exception {
        _ctx.setup(configfile);
    }

    //~--- get methods --------------------------------------------------------

    /**
     * Get a handle to the current Context.
     * @return
     */
    public static final Context get() {
        return _ctx;
    }

    //~--- methods ------------------------------------------------------------

    private void setup(String configfile) throws Exception {
        net.langene.common.Context.create(configfile);
        _logger = LoggerFactory.getLogger("net.langene.nlp");
        _logger.info("Langene Core Context initialized using configuration [" + configfile + "]");

        String	prop = getConfig().getString("net.langene.core.language");

        if ((prop != null) &&!prop.isEmpty()) {
            language = prop;
        }

        dbsetup();
    }

    private void dbsetup() throws Exception {
        String	dbhost = getConfig().getString("net.langene.core.db.host");

        if ((dbhost == null) || dbhost.isEmpty()) {
            throw new Exception(
                "Invalid Configuration : Missing parameter [net.langene.core.db.host]");
        }

        String	dbuser = getConfig().getString("net.langene.core.db.username");

        if ((dbuser == null) || dbuser.isEmpty()) {
            throw new Exception(
                "Invalid Configuration : Missing parameter [net.langene.core.db.username]");
        }

        String	dbpasswd = getConfig().getString("net.langene.core.db.password");

        if ((dbpasswd == null) || dbpasswd.isEmpty()) {
            throw new Exception(
                "Invalid Configuration : Missing parameter [net.langene.core.db.password]");
        }

        Class.forName("com.mysql.jdbc.Driver");
        dbconn = DriverManager.getConnection("jdbc:mysql://" + dbhost, dbuser, dbpasswd);
        dbconn.setAutoCommit(true);
        DBSetup.checkAndInstallDB();
    }

    //~--- get methods --------------------------------------------------------

    /**
     * Get a handle to the application configuration.
     *
     * @return
     */
    public XMLConfiguration getConfig() {
        return net.langene.common.Context.get().getConfig();
    }

    /**
     * Get the Database Connection.
     *
     * @return
     */
    public Connection getConnection() {
        return dbconn;
    }

    /**
     * @return the language
     */
    public String getLanguage() {
        return language;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
